

<template>
  <div class="execution">
    <basic-container>
      <avue-crud
        ref="crud"
        v-model="form"
        :table-loading="loading"
        :page="page"
        :data="data"
        :option="formOption"
        @size-change="sizeChange"
        @current-change="currentChange"
        @refresh-change="refreshChange"
        @search-change="searchChange"
        @search-reset="searchReset"
        @selection-change="selectionChange"
        @on-load="onLoad">

        <template slot="menuLeft">
          <el-button type="primary"
                     size="small"
                     icon="el-icon-edit"
                     plain
                     @click="handleImportTable">导入表
          </el-button>
        </template>

      </avue-crud>

    </basic-container>
  </div>
</template>

<script>
  import {listDbTable, importTable} from '@/api/gen/gen';
  import {formOption} from '@/const/crud/gen/gen';
  import {fetchAllList} from "@/api/gen/datasource";
  import {mapGetters} from "vuex";

  export default {
    data() {
      return {
        data: [],
        page: {
          total: 0, // 总页数
          currentPage: 1, // 当前页数
          pageSize: 10 // 每页显示多少条
        },
        selectionList: [],
        form: {},
        query: {},
        loading: false,
        formOption: formOption
      }
    },
    computed: {
      ...mapGetters(["permissions"]),
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      },
      tables() {
        let tables = [];
        this.selectionList.forEach(ele => {
          tables.push(ele.tableName);
        });
        return tables.join(",");
      }
    },
    created() {
      this.getDataSourceList()
    },
    methods: {

      onLoad(page, params) {
        listDbTable(Object.assign({
          current: page.currentPage,
          size: page.pageSize
        }, params, this.query)).then(response => {
          this.data = response.data.data.records
          this.page.total = response.data.data.total
        })
      },

      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1;
        this.onLoad(this.page, params);
        done();
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      refreshChange() {
        this.onLoad(this.page,this.query)
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      sizeChange(pageSize) {
        this.page.pageSize = pageSize
      },
      currentChange(current) {
        this.page.currentPage = current
      },
      handleImportTable() {
        importTable(this.tables, this.query.dataSource).then(res => {
          this.$notify.success(res.data.msg);
          if (res.status === 200) {
            this.$emit("getList");
          }
        });
      },
      getDataSourceList() {
        fetchAllList().then(response => {
          const column = this.findObject(this.formOption.column, "dataSource");
          column.dicData = response.data.data;
        })
      },

    }
  };
</script>

<style lang="scss" scoped>
</style>

